import hashlib
from datetime import datetime
from django.db import models
from shortuuidfield import ShortUUIDField

GENDER_CHOICES = (
    (u'0', u'正常'),
    (u'1', u'注销'),
)


# 用户表
class UserModel(models.Model):
    u_id = ShortUUIDField(primary_key=True, verbose_name="用户id")
    u_phone = models.CharField(unique=False, max_length=11, null=False, blank=False, verbose_name="手机号")
    u_name = models.CharField(null=False, blank=False, max_length=32, verbose_name="用户名")
    u_password = models.CharField(verbose_name="密码", null=False, blank=False, max_length=64)
    u_IDcard = models.CharField(max_length=18, unique=True, blank=True, null=True, verbose_name="身份证")
    u_address = models.CharField(blank=True, null=True, max_length=128, verbose_name='住址')
    u_register_time = models.CharField(default=datetime.now().strftime("%Y-%m-%d %H:%M:%S"), max_length=20,
                                       verbose_name="创建日期")
    u_login_time = models.DateTimeField(auto_now_add=True, verbose_name='最后一次登录时间')
    u_status = models.CharField(max_length=2, choices=GENDER_CHOICES, default='0', verbose_name="是否注销")
    emergency_contact = models.CharField(max_length=11, verbose_name='紧急联系人电话', null=False, blank=False)

    # u_image = models.ImageField(verbose_name='头像', upload_to='users/avatars/', blank=True, null=True, default='')

    def save(self, *args, **kwargs):
        self.u_password = hashlib.sha256(self.u_password.encode("utf-8")).hexdigest()
        super(UserModel, self).save(*args, **kwargs)

    class Meta:
        db_table = 'user_user'
        verbose_name = '用户表'
        verbose_name_plural = verbose_name


# 用户家属表
class RelationModel(models.Model):
    r_id = ShortUUIDField(primary_key=True, verbose_name="用户id")
    r_name = models.CharField(null=False, blank=False, max_length=32, verbose_name="用户名")
    r_ID_card = models.CharField(max_length=18, unique=True, blank=True, null=True, verbose_name="身份证")
    r_address = models.CharField(blank=True, null=True, max_length=128, verbose_name='住址')
    r_status = models.CharField(max_length=2, choices=GENDER_CHOICES, default='0', verbose_name="是否注销")

    class Meta:
        db_table = 'user_relation'
        verbose_name = '用户家属表'
        verbose_name_plural = verbose_name


# 用户家属关联表
class AssociatedModel(models.Model):
    r_id = ShortUUIDField(primary_key=True, verbose_name="用户id")
    user_id = models.CharField(max_length=18, blank=True, null=True, verbose_name="用户身份证号")
    associated_id = models.CharField(max_length=18, blank=True, null=True, verbose_name="关联用户身份证号")
    associated = models.CharField(max_length=18, blank=True, null=True, verbose_name="关联用户关系")

    class Meta:
        db_table = 'user_associated'
        verbose_name = '用户家属关联表'
        verbose_name_plural = verbose_name


# 用户家属关联表
class RelevanceModel(models.Model):
    r_id = ShortUUIDField(primary_key=True, verbose_name="关联表id")
    user_id = models.CharField(max_length=18, blank=True, null=True, verbose_name="用户身份证号")
    associated_id = models.CharField(max_length=18, blank=True, null=True, verbose_name="关联用户身份证号")
    associated_name = models.CharField(max_length=18, blank=True, null=True, verbose_name="关联用户姓名")
    associated = models.CharField(max_length=18, blank=True, null=True, verbose_name="关联用户关系")
    r_address = models.CharField(blank=True, null=True, max_length=128, verbose_name='关联用户住址')
    r_status = models.CharField(max_length=2, choices=GENDER_CHOICES, default='0', verbose_name="是否解除关联")

    class Meta:
        db_table = 'relevance'
        verbose_name = '用户家属关联表'
        verbose_name_plural = verbose_name


# 投诉建议
class ComplainModel(models.Model):
    c_id = ShortUUIDField(primary_key=True, verbose_name="投诉id")
    c_complain = models.TextField(verbose_name="投诉建议")
    c_title = models.CharField(max_length=128, verbose_name="标题")
    c_image = models.ImageField(upload_to='static/complain', verbose_name="图片")
    c_time = models.CharField(default=datetime.now().strftime("%Y-%m-%d %H:%M:%S"), max_length=20,
                              verbose_name="投诉日期")
    c_IDcard = models.CharField(max_length=18, blank=True, null=True, verbose_name="身份证")
    c_status = models.CharField(max_length=2, choices=GENDER_CHOICES, default='0', verbose_name="是否删除")
    c_reply = models.CharField(max_length=2, choices=GENDER_CHOICES, default='0', verbose_name="是否回复")

    class Meta:
        db_table = 'user_complain'
        verbose_name = '投诉建议表'
        verbose_name_plural = verbose_name


# 黑名单
class BlacklistModel(models.Model):
    GENDER_CHOICES_BLACKlLIST = (
        (u'0', u'不移除'),
        (u'1', u'移除'),
    )

    b_id = ShortUUIDField(primary_key=True, verbose_name="黑名单id")
    user_id_card = models.CharField(max_length=18, verbose_name='用户身份证号')
    default_number = models.CharField(max_length=3, verbose_name='违约次数')
    default_status = models.CharField(max_length=2, choices=GENDER_CHOICES_BLACKlLIST, default='0',
                                      verbose_name="是否移除黑名单")

    class Meta:
        db_table = 'blacklist'
        verbose_name = '用户黑名单'
        verbose_name_plural = verbose_name



